package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.AbilitaManagerRemote;
import staticHtml.PaginaStatica;
import entities.Utente;

public class RicercaAiuto extends HttpServlet{
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		mostraRicerca(request, response);
	}
	
	private void mostraRicerca(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			Hashtable<String,String> env = new Hashtable<String,String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL,"localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = jndiContext.lookup("AbilitaManager/remote");
			AbilitaManagerRemote r = (AbilitaManagerRemote) ref;
	
			response.setContentType("text/html");

			PrintWriter out = response.getWriter();
			
			
			Utente utente=(Utente) request.getSession().getAttribute("utente");
			
			if(utente==null)
			{
				out.println(PaginaStatica.top());
				out.println("	<div id='titolo'>"+
						"<label class='titolo'> Ricerca aiuto</label></div>");
				out.println("<form method='post' action='elencoUtentiAbilita.html'>");
				out.println("<input type='radio' name='scelta' value='amici' DISABLED> Amici");
			}
			else
			{
				out.println(PaginaStatica.topUser());
				out.println("	<div id='titolo'>"+
						"<label class='titolo'> Ricerca aiuto</label></div>");
				out.println("<form method='post' action='elencoUtentiAbilita.html'>");
				out.println("<input type='radio' name='scelta' value='amici'> Amici");
			}
			
			
			out.println("<input type='radio' name='scelta' value='tutti' checked='checked'> Tutti");
			
			List<String> abilita=r.getAllAbilita();
			out.println("<select name='abilita'>");
			for(String a: abilita){
				out.println("<option value='"+a+"'> "+a+" </option>");
			}
			out.println("</select><input type='submit' value='cerca'></form></body><html>");
			
	} catch (NamingException e) {
		e.printStackTrace();
	}
}

}
